package indi.kider.server.woniubus.util;

import com.alibaba.fastjson.JSONObject;
/*
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
*/

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtil {	
	/**
	 * 读取excel文件2007
	 */
	public static List<JSONObject> readExcel(InputStream is,String [] names,boolean isExcel2003) throws Exception{
		List<JSONObject> list = new ArrayList<JSONObject>();
		/*
		try {
			Workbook xssfWorkbook = null;
			if(isExcel2003){
				xssfWorkbook = new HSSFWorkbook(is);
			} else {
				xssfWorkbook = new XSSFWorkbook(is);
			}
			for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
				Sheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
				if (xssfSheet == null) {
					continue;
				}
			    for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
			    	Row xssfRow = xssfSheet.getRow(rowNum);
			    	if(xssfRow == null){
			    		continue;
			    	}
					JSONObject nobj = new JSONObject();  
			    	for (int i = 0; i < xssfRow.getLastCellNum(); i++) {
			    		try {
			    			Cell cell = xssfRow.getCell(i);
					    	String cellValue = "";
				    	    if(cell != null){				    	    	
				    	    	 switch (cell.getCellType()){
				    	    	 case Cell.CELL_TYPE_NUMERIC:  
					                if (HSSFDateUtil.isCellDateFormatted(cell)) {
					                	cellValue = new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue());  
					                } else { // 纯数字  
					                	cellValue = String.valueOf(cell.getNumericCellValue());  
					                }  
				                    break; 
				    	    	 default:
					    			cell.setCellType(Cell.CELL_TYPE_STRING);				    			
					    			cellValue = cell.getStringCellValue();
								}
				    		}
							nobj.put(names[i],cellValue); 
						} catch (Exception e) {
							continue;
						}
			    	}
			    	list.add(nobj);
			    }
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		*/
		return list;
	} 

	// @描述：是否是2003的excel，返回true是2003
	public static boolean isExcel2003(String filePath)  {
		return filePath.matches("^.+\\.(?i)(xls)$");
	}

	//@描述：是否是2007的excel，返回true是2007
	public static boolean isExcel2007(String filePath)  {
		return filePath.matches("^.+\\.(?i)(xlsx)$");
	}

}
